chore(nextjs): Fix server actions detection - #3995
Conversation
`isServerActionRequest` was still depending on the `next-url` header which no longer is available (removed at `14.1.0` for server actions and in `14.2.2` when requesting RSCs. This PR fixes the issue and prepares the ground for future work that would require these utilities to work properly. `pagePath` from __nextGetStaticStore is available since `next@13.5.4` until `next@14.2.5` which is the latest stable release
🦋 Changeset detectedLatest commit: 128e3c8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
| return ( | ||
| !!req.headers.get(nextConstants.Headers.NextUrl) && | ||
| (req.headers.get(constants.Headers.Accept)?.includes('text/x-component') || | ||
| req.headers.get(constants.Headers.ContentType)?.includes('multipart/form-data') || |
There was a problem hiding this comment.
Checking against multipart is not realistic. The following example will fire a request with Content-type text/plain;charset=UTF-8
'use client';
import { addItem2 } from '../actions';
export default function AddToCart() {
return (
<button
type='button'
onClick={() => {
addItem2({ some: 'data' });
}}
>
Click
</button>
);
}|
|
||
| const isServerActionRequest = (req: Request) => { | ||
| return ( | ||
| !!req.headers.get(nextConstants.Headers.NextUrl) && |
There was a problem hiding this comment.
Next-url as a header has been dropped from request to server actions since 14.1.0 way before 14.2.2 where our page detection started to fail.
| * github.com/vercel/next.js/blob/0ac10d79720cc950df96bd9d4958c9be0c075b6f/packages/next/src/lib/is-app-route-route.ts | ||
| * In case we want to handle router handlers and server actions differently in the future | ||
| */ | ||
| export function isAppRouteRoute(route: string): boolean { |
There was a problem hiding this comment.
FWIW, I think they are called "route handlers": https://nextjs.org/docs/app/building-your-application/routing/route-handlers
| export function isAppRouteRoute(route: string): boolean { | |
| export function isAppRouteHandler(route: string): boolean { |
| * *Attention*: | ||
| * When used within the Edge Middleware this utility will mistakenly detect a Route Handler as a Page |
There was a problem hiding this comment.
Is this problematic for our middleware in any scenarios you can think of?
There was a problem hiding this comment.
@panteliselef related to Bryce's question above, what does this comment mean for the middleware's auth() helper?
| "@clerk/nextjs": patch | ||
| --- | ||
|
|
||
| Fix server actions detection |
There was a problem hiding this comment.
Can we please state what's the impact for the end user here?
| * *Attention*: | ||
| * When used within the Edge Middleware this utility will mistakenly detect a Route Handler as a Page |
There was a problem hiding this comment.
@panteliselef related to Bryce's question above, what does this comment mean for the middleware's auth() helper?
|
Since we did not have any issue reports and seems like we will not be needing this in the near future, I will be closing this. We can always come back and reopen |
Description
isServerActionRequestwas still depending on thenext-urlheader which no longer is available (removed at14.0.4for server actions and in14.2.3when requesting RSCs (Related PR)This PR fixes the issue and prepares the ground for future work that would require these utilities to work properly.
pagePathfrom __nextGetStaticStore is available sincenext@13.5.4untilnext@14.2.5which is the latest stable releaseChecklist
npm testruns as expected.npm run buildruns as expected.Type of change